
#include "queue.cpp"

#define irPin 0
#define ledPin 13

int led_state = LOW;
volatile boolean busy = false;
unsigned long  now_time;
unsigned long  tx_time;
unsigned long  tx_cnt;

queue_16 ir_que;
 
void setup() {
  pinMode(ledPin,OUTPUT);
  attachInterrupt(irPin,ir_rec,CHANGE);
  Serial.begin(9600);

  ir_que.reset();

  digitalWrite(ledPin,HIGH);
  delay(1000);
  digitalWrite(ledPin,LOW);
  Serial.println("Starting learning");
}
 
void loop() {
  int depth=0;
  tx_time = 0;
  depth = ir_que.get_depth();

  if(depth > 0) {
    //Serial.println(depth,DEC);
    ir_que.pop(tx_time);
    busy = true;
    Serial.print(ir_que.cnt,DEC);
    Serial.print(":");
    Serial.println(tx_time,DEC);
    busy = false;
  }
  else {
    delayMicroseconds(1);
  }
}

void ir_rec() {
  now_time = micros();
  //if(busy == true) {
  //  ir_que.retreat();
  //}
  ir_que.push(now_time);
}
